package za.ac.wits.was.research;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import za.ac.wits.was.user.model.AcademicStaff;

/**
 *
 * @author Salman Noor : School of Electrical and Information Engineering
 * @Copyright: Wits University 2013 - All Rights Reserved
 * @email: salmannoor74415@gmail.com
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 */
@Service
@Transactional
public class ResearchAllocationsService {

    private SessionFactory sessionFactory;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public List<ResearchAllocations> getActiveResearchAllocations(Long discipline_id){
        Session sess = this.sessionFactory.openSession();
        List<ResearchAllocations> ra = sess.createQuery("from ResearchAllocations ra where ra.academic_staff_member.discipline.discipline_id = :disciplineID AND pending = FALSE AND approved = TRUE").setParameter("disciplineID", discipline_id).list();
        if (ra == null){
            return new ArrayList();
        }
        for(ResearchAllocations r : ra){
        r.getAcademic_staff_member();
        }
        sess.flush();
        sess.close();
        return ra;
    }
    public List<ResearchAllocations> getPendingResearchAllocations(){
    Session sess = this.sessionFactory.openSession();
    List<ResearchAllocations> pendingAllocations = sess.createQuery("from ResearchAllocations where pending = TRUE").list();
    if (pendingAllocations == null){
    return new ArrayList<ResearchAllocations>();
    }
    for(ResearchAllocations ra: pendingAllocations){
    ra.getAcademic_staff_member();
    }
    sess.flush();
    sess.close();
    return pendingAllocations;
    }
    public ResearchAllocations createNewResearchAllocation(String uname, String resName, String resDescription, Integer resHours, Boolean resSabb, Date resStartDate, Date resEndDate, Date sabbStartDate, Date sabbEndDate) {
        Session sess = this.sessionFactory.openSession();
        AcademicStaff academicStaffUser = (AcademicStaff) sess.createQuery("from was_user where username = :username ").setParameter("username", uname).uniqueResult();
        ResearchAllocations newResearchAllocation = new ResearchAllocations();
        newResearchAllocation.setAcademic_staff_member(academicStaffUser);
        newResearchAllocation.setResearch_topic(resName);
        newResearchAllocation.setResearch_description(resDescription);
        newResearchAllocation.setResearch_hours(resHours);
        newResearchAllocation.setResearch_sabbatical(resSabb);
        newResearchAllocation.setResearch_start_date(resStartDate);
        newResearchAllocation.setResearch_end_date(resEndDate);
        newResearchAllocation.setSabbatical_start_date(sabbStartDate);
        newResearchAllocation.setSabbatical_end_date(sabbEndDate);
        
        newResearchAllocation.setApproved(false);
        newResearchAllocation.setPending(true);
        
        sess.save(newResearchAllocation);
        sess.flush();
        sess.close();
        return newResearchAllocation;
    }
    
    public ResearchAllocations approveResearch(Long research_allocation_id){
        Session sess = this.sessionFactory.openSession();
        ResearchAllocations thePendingResearch = (ResearchAllocations) sess.get(ResearchAllocations.class, research_allocation_id);
        thePendingResearch.setApproved(Boolean.TRUE);
        thePendingResearch.setPending(Boolean.FALSE);
        sess.saveOrUpdate(thePendingResearch);
        sess.flush();
        sess.close();
        return thePendingResearch;
    }
    
    public ResearchAllocations declineResearch(Long research_allocation_id){
        Session sess = this.sessionFactory.openSession();
        ResearchAllocations thePendingResearch = (ResearchAllocations) sess.get(ResearchAllocations.class, research_allocation_id);
        thePendingResearch.setApproved(Boolean.FALSE);
        thePendingResearch.setPending(Boolean.FALSE);
        sess.saveOrUpdate(thePendingResearch);
        sess.flush();
        sess.close();
        return thePendingResearch;
    }
    
}
